package com.ohos.carbon.slice.animations;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Map;

import static com.ohos.carbon.ResourceTable.Media_mazda;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class TouchRippleSlice extends AbilitySlice {

    private Image back_button, image1, image2, imageMazda, imageView;
    private Text text_title, btnBorderless;
    private Button btnBackground, btnRadius, btnOver;
    private DependentLayout dep, imgDtl;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_touch_ripple);
        initView();
        initDate();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Touch ripple")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Touch ripple")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Touch ripple", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Touch ripple", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Touch ripple", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        //标题
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Touch ripple");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
    }

    private void initDate() {
        //初始化
        btnBackground = (Button) findComponentById(ResourceTable.Id_btn_background);
        btnRadius = (Button) findComponentById(ResourceTable.Id_btn_radius);
        btnBorderless = (Text) findComponentById(ResourceTable.Id_text_borderless);
        btnOver = (Button) findComponentById(ResourceTable.Id_btn_over);
        imageMazda = (Image) findComponentById(ResourceTable.Id_image_mazda);
        imageView = (Image) findComponentById(ResourceTable.Id_image_view);
        dep = (DependentLayout) findComponentById(ResourceTable.Id_dep);
        imgDtl = (DependentLayout) findComponentById(ResourceTable.Id_img_dtl);
        DirectionalLayout.LayoutConfig ballScaleIndicatorConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);
        ballScaleIndicatorConfig.setMargins(13, 0, 0, 0);
        BallScaleIndicator ballScaleIndicator = new BallScaleIndicator(this);
        ballScaleIndicator.setLayoutConfig(ballScaleIndicatorConfig);
        ballScaleIndicator.setVisibility(Component.INVISIBLE);
        dep.addComponent(ballScaleIndicator);
        // BACKGROUND 点击事件(first)
        btnBackground.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ballScaleIndicator.start();
                ballScaleIndicator.setVisibility(Component.VISIBLE);
            }
        });
        //RADIUS = 50DP 触摸事件监听器(按下/抬起)（two）
        btnRadius.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        btnRadius.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_bg_color_lightgreen));
                        break;
                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        btnRadius.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_bg_color_green));
                        break;
                }
                return true;
            }
        });
        // BORDERLESS 触摸事件监听器(按下/抬起)（three）
        btnBorderless.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        imageView.setVisibility(Component.VISIBLE);
                        break;
                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        imageView.setVisibility(Component.HIDE);
                        break;
                }
                return true;
            }
        });
        //OVER 触摸事件监听器(按下/抬起)（four）
        btnOver.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        btnOver.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_bg_color_lightgreen));
                        break;
                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        btnOver.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_bg_color_green));
                        break;
                }
                return true;
            }
        });
        //image
        imageMazda.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        imgDtl.setAlpha(0.5f);
                        break;
                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        imgDtl.setAlpha(1);
                        imageMazda.setPixelMap(Media_mazda);
                        break;
                }
                return true;
            }
        });
        //点击返回上个页面
        back_button.setClickedListener(component -> {
            onBackPressed();
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

